﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Cs.Multilingual
{
    /// <summary>
    /// Interaction logic for Editor.xaml
    /// </summary>
    public partial class Editor : Window
    {
        public Editor()
        {
            InitializeComponent();
            foreach (var language in Lang.Languages)
            {
                LangName.Items.Add(language);
            }
        }

        private void AddLanguage_Click(object sender, RoutedEventArgs e)
        {
            var langC = new LangControl();
            LangData.Children.Add(langC);
            langC.LangName.Text = LangName.Text;
            Refresh_Click(null, null);
        }

        private void RemoveLanguage_Click(object sender, RoutedEventArgs e)
        {
            do
            {
                var ch = from object child in LangData.Children
                     let obj = child as LangControl
                     where obj != null && obj.LangName.Text == LangName.Text
                     select child;

                var child1 = ch as IList<object> ?? ch.ToList();
                if (child1.Any())
                    LangData.Children.Remove((UIElement) child1.First());
                else break;
            } while (true);
            Refresh_Click(null, null);
        }


        private void AllId_TextChanged(object sender, TextChangedEventArgs e)
        {
            foreach (var obj in LangData.Children.OfType<LangControl>())
            {
                obj.Id.Text = AllId.Text;
            }
        }

        private void RecordAll_Click(object sender, RoutedEventArgs e)
        {
            foreach (var obj in LangData.Children.OfType<LangControl>())
            {
                obj.Button_Click(sender, e);
            }
            Refresh_Click(null, null);
        }

        private void Refresh_Click(object sender, RoutedEventArgs e)
        {

            IdList.Items.Clear();
            var list = new List<string>();
            foreach (
                var id in
                    LangData.Children.OfType<LangControl>()
                            .Select(obj => new Lang(obj.LangName.Text))
                            .SelectMany(lang => lang.Ids.Where(id => !list.Contains(id))))
            {
                list.Add(id);
            }
            foreach (var item in list)
            {
                IdList.Items.Add(item);
            }

        }

        private void IdList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            AllId.Text = IdList.SelectedItem as string;
        }


    }
}
